package org.jeecg.glr.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.glr.entity.GlrLight;
import org.jeecg.glr.mapper.GlrLightMapper;
import org.jeecg.glr.mapper.GlrLightPoleDeviceMapper;
import org.jeecg.glr.service.IGlrGatewayService;
import org.jeecg.glr.service.IGlrLightPoleDeviceService;
import org.jeecg.glr.service.IGlrLightService;
import org.jeecg.glr.util.GLRConstant;
import org.jeecg.glr.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 路灯管理
 * @Author: jeecg-boot
 * @Date: 2020-03-05
 * @Version: V1.0
 */
@Service
public class GlrLightServiceImpl extends ServiceImpl<GlrLightMapper, GlrLight> implements IGlrLightService {

    @Resource
    GlrLightMapper glrLightMapper;

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private IGlrLightPoleDeviceService glrLightPoleDeviceService;
    @Autowired
    private IGlrGatewayService glrGatewayService;

    @Override
    public void saveLight(GlrLight glr) {
        if (StringUtil.isEmpty(glr.getId())) {
            String code = glrLightMapper.getMaxLightCodeByGateWayCode(glr.getGateWayCode());
            int lig_code = 1;
            if (StringUtil.isNotEmpty(code)) {
                lig_code = Integer.parseInt(code)+1;
            }
            code = String.format("%04d", lig_code);
            glr.setCode(code);
        }
        super.save(glr);
        glrLightPoleDeviceService.dataProecss(glr,GLRConstant.GLR_LIGHT_POLE_DEVICE_TYPE_ADD);
    }

    @Override
    public  void saveImportLight(GlrLight glrLight){
        super.save(glrLight);
        glrLightPoleDeviceService.dataProecss(glrLight,GLRConstant.GLR_LIGHT_POLE_DEVICE_TYPE_ADD);
    }

    @Override
    public void updateEntityById(GlrLight glr){
        glrLightPoleDeviceService.dataProecss(glr,GLRConstant.GLR_LIGHT_POLE_DEVICE_TYPE_EDIT);
        glrLightMapper.updateById(glr);
    }

    @Override
    public void removeEntityById(String id){
        GlrLight glr=glrLightMapper.selectById(id);
        glrLightMapper.deleteById(id);
        glrLightPoleDeviceService.dataProecss(glr,GLRConstant.GLR_LIGHT_POLE_DEVICE_TYPE_DELETE);
    }

    @Override
    public void removeEntityByIds(List<String> asList){
        for(String stId:asList){
            GlrLight glr=glrLightMapper.selectById(stId);
            glrLightMapper.deleteById(stId);
            glrLightPoleDeviceService.dataProecss(glr,GLRConstant.GLR_LIGHT_POLE_DEVICE_TYPE_DELETE);
        }
    }

    @Override
    public void deleteByGateWayCode(String code) {
        glrLightMapper.deleteByGateWayCode(code);
    }

    @Override
    public int getLightCount() {
        String sql="select count(*) from glr_light where 1=1";
       /* if(!SysUtil.getUserInfo().getUserType().equals(SysConstant.SYS_USER_TYPE_ADMIN)){
                sql=sql+" and sys_company_code='"+SysUtil.getUserInfo().getSysCompanyCode()+"'";
        }*/
        return glrLightMapper.getLightCount(sql);
    }

}
